package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.TdlstjAdapter
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.databinding.ActivityTdLstjBinding
import client.comm.baoding.ui.vm.TdViewModel
import client.comm.commlib.widget.EmptyLayout

/**
 * 代理历史推荐
 */
class TdlstjActivity : BaseMiddleActivity<TdViewModel, ActivityTdLstjBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_td_lstj
    }

    val adapter: TdlstjAdapter by lazy {
        TdlstjAdapter(this, mImgLoader)
    }

    override fun initView() {
        binding.event = this
        binding.recyclerView.adapter = adapter
//        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))
        binding.recyclerView.onLoadMore = {
            viewModel.get_user_team1()
        }
        binding.refreshLayout.setOnRefreshListener {
            viewModel.pageTwo.value = 0
            viewModel.get_user_team1()
        }
        binding.emptyLayout.setOnClickListener {
            viewModel.get_user_team1()
        }

        viewModel.callTwo.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list = it.data!!.sub_list as MutableList<TdBean.Sub?>
                    adapter.submitData(list, it.data!!)

                    binding.title = "${it.data?.nickname}的代理"
                    if (list == null || list.size < 10) {
                        adapter.hasMoreState.value = false
                    }

                    binding.emptyLayout.visibility = View.GONE
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
                binding.refreshLayout.isRefreshing = false
            }
        })
    }

    override fun initData() {
        intent.getStringExtra("bean").apply {

            viewModel.bean.value = this
            viewModel.get_user_team1()
        }
    }

    override fun initViewModel(): TdViewModel = createViewModel()

    /**
     * 奖励记录
     */
    fun jljl(){
        startActivity(Intent(this, TdJlJlActivity::class.java))
    }

}